package com.movieSystem.view;

import com.movieSystem.model.UserInfo;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import tcpFirmware.OKClient;
import tcpFirmware.Response;
//import tcpFirmware.Serial;
import tcpFirmware.Type;
import tcpFirmware.model.DBHelper;

import javax.swing.*;
import java.io.*;

import java.util.HashMap;

import static javafx.application.Platform.exit;

public class MovieTime extends Application {


    @Override
    public void start(Stage primaryStage) {

        try {

            Scene scene = new Scene(this.initScene(),1200,800);
            primaryStage.setResizable(false);   //窗口大小不可变
            primaryStage.setScene(scene);   //根节点
            primaryStage.show();    //展示
            primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent event) {
                    Platform.exit();
                    System.exit(0);
                }
            });
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setHeaderText("加载中");
            alert.setContentText("请稍等");
            alert.show();
            MenuBar menuBar = new MenuBar();
            Menu menuUser = new Menu("用户");
            MenuItem itemU = new MenuItem("我的信息");
            itemU.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    if(FrmUser.first){
                        FrmUser fUser =new FrmUser(primaryStage);
                        fUser.setVisible(true);
                        FrmUser.first=false;
                    }
                }
            });
            menuUser.getItems().add(itemU);

            MenuItem itemAdmin;
            if(UserInfo.getIsAdmin().equals("1")){itemAdmin = new MenuItem("上架电影");
            itemAdmin.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    AdminMovieView view = new AdminMovieView();
                    view.setVisible(true);
                }
            });
            menuUser.getItems().add(itemAdmin);
            }

            menuBar.getMenus().add(menuUser);
            ((VBox)scene.getRoot()).getChildren().add(0,menuBar);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    GridPane gridPane = new GridPane();//全局变量
    HashMap<String,String> nameList = new HashMap<>();

    public Parent initScene(){
        VBox vBox = new VBox();

        gridPane.setHgap(10);
        gridPane.setVgap(10);



        //顶部文字展示
        StackPane p = new StackPane();
        Text text = new Text("热门电影");
        text.setFont(new Font(30));
        p.setAlignment(Pos.TOP_CENTER); //顶部居中
        p.getChildren().add(text);


        //滚动面板
        ScrollPane scrollPane = new ScrollPane();



        scrollPane.setContent(gridPane);




        try {
            OKClient okClient1 = new OKClient(new Response() {
                @Override
                public void onResponse(HashMap<String, String> header, byte[] dataBytes) {
                    nameList = OKClient.dealWithForm(dataBytes);
                    int j=0;
                    for(String i:nameList.keySet()){
                        int k = Integer.parseInt(i)%4;
                    try {

                        int finalJ = j;
                        OKClient okClient = new OKClient(new Response() {   //构造请求
                            @Override
                            public void onResponse(HashMap<String, String> header, byte[] dataBytes) {
                                String img = header.get("img");
                                String movieName = header.get("movieName");
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        ImageView imageView = new ImageView(new Image(new ByteArrayInputStream(OKClient.downloadImage(img))));
                                        imageView.setFitHeight(400);
                                        imageView.setFitWidth(280);
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                //更新JavaFX的主线程的代码放在此处
                                                //                                            FileInputStream inputStream = new FileInputStream(header.get("img"));

                                                //空白面板
                                                VBox vbox = new VBox();
                                                vbox.setBackground(new Background(new BackgroundFill(Paint.valueOf(Color.LIGHTPINK.toString()), new CornerRadii(10), Insets.EMPTY)));
                                                vbox.setAlignment(Pos.CENTER);
                                                Text textSession = new Text(movieName);
                                                Button btnBuy1 = new Button("详情");
                                                //按钮跳转新页面
                                                btnBuy1.setOnMouseClicked((event)->{
                                                    MovieMoment movieMoment = new MovieMoment(imageView.getImage(),movieName);
                                                    movieMoment.start(new Stage());
                                                });
                                                vbox.setMaxWidth(280);
                                                vbox.setMaxHeight(800);

                                                //把详情按钮扔进空白面板
                                                vbox.getChildren().addAll(imageView,textSession,btnBuy1);
                                                gridPane.add(vbox,k, finalJ);

                                            }
                                        });
                                    }
                                }).start();

                            }
                        });
                        okClient.setRoute("mvDetail").setType(Type.text.type).setHeader("mvName",nameList.get(i));
                        okClient.sendTo();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(k==3)j++;
                    }
                }
            });
            okClient1.setRoute("getAllMvName").setType(Type.text.type);
            okClient1.sendTo();
        } catch (IOException e) {
            e.printStackTrace();
        }


        vBox.getChildren().addAll(p,scrollPane);
        return vBox;
    }

    public Node getRight(){
        return null;
    }









    public static void open(String...args) {

        FrmLogin.javaFxFirst=false;
        launch(args);
    }




}
